﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ChronoNES
{
    class PPU
    {
        byte ppuControlRegister1;
        byte ppuControlRegister2;
        byte ppuStatusRegister;
        byte sRAMAddress;
        byte vRAMAddress;
        byte[] sRAM; // sprite ram
        byte[] vRAM; // video ram

        byte[] scrollOrigin;
        int scrollWrite;

        public PPU()
        {
            vRAM = new byte[2 * 1024];  // 2 KB
            sRAM = new byte[256];       // 256 B
            scrollOrigin = new byte[2]; // 0=horizontal; 1=vertical
            scrollWrite = 0;          //horizontal

            Reset();
        }

        public void Reset()
        {
        }

        public byte Read(ushort _address)
        {
            switch (_address)
            {
                case 0x02: return ppuStatusRegister;
                case 0x04: return sRAM[sRAMAddress];
                case 0x07: return vRAM[vRAMAddress];

                default:
                    throw new Exception("Unsupported PPU read");
            }
        }
        public void Write(ushort _address, byte _value)
        {
            switch (_address)
            {
                case 0x00: ppuControlRegister1 = _value; break;
                case 0x01: ppuControlRegister2 = _value; break;
                case 0x03: sRAMAddress = _value;  break;
                case 0x04: sRAM[sRAMAddress] = _value; break;
                case 0x05: scrollOrigin[scrollWrite] = _value; scrollWrite = (scrollWrite == 1 ? 0 : 1); break;
                case 0x06: vRAMAddress = _value; break;
                case 0x07: vRAM[vRAMAddress] = _value; break;

                default:
                    throw new Exception("Unsupported PPU Write");
            }
        }
    }
}
